﻿using ZYCommon;
using System;
using System.Collections;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using TextEditor.TextDocument.Document;

namespace TextEditor
{
    public class ZYTextContainer : ZYTextElement
    {
        protected bool bolLocked = false;
        protected int intBottomMargin = 0;
        protected int intClientWidth = 0;               //长度
        protected Color intForeColor = SystemColors.WindowText;
        protected int intLeftMargin = 0;
        protected int intLineSpan = 1;
        protected int intMaxWidth = 0;
        protected int intRightMargin = 0;
        protected int intTopMargin = 0;
        protected ArrayList myChildElements = new ArrayList(100);
        private ArrayList myContentList = new ArrayList(100);
        protected ZYTextElement myLastElement = null;
        protected ArrayList myLines = new ArrayList();
        private string strID;
        private string strName;
        private SizeF TitleSize;

        public int IntLength
        {
            get
            {
                return this.ToZYString().Trim().Length;
            }
        }

        /***
         * 构造函数
         */
        public ZYTextContainer(int i)
        {
        }

        public ZYTextContainer()
        {
            base.myBorder = new ZYTextBorder();
            this.myLastElement = new ZYTextParagraph();
            this.myLastElement.Parent = this;
            this.AddLastElement();
            base.Visible = true;
            base.Index = 0;
        }

        public virtual void AddElementToList(ArrayList myList, bool ResetFlag)
        {
            if (myList != null)
            {
                foreach (ZYTextElement element in this.myChildElements)
                {
                    if (ResetFlag)
                    {
                        element.Visible = false;
                        element.Index = -1;
                    }
                    if (base.myOwnerDocument.isVisible(element))
                    {
                        if (!(element is ZYTextContainer))
                        {
                            myList.Add(element);
                        }
                        else
                        {
                            element.Visible = true;
                            (element as ZYTextContainer).AddElementToList(myList, ResetFlag);
                        }
                    }
                }
            }
        }

        public virtual void AddElementToListAbs(ArrayList myList)
        {
            foreach (ZYTextElement element in this.myChildElements)
            {
                myList.Add(element);
                if (element is ZYTextContainer)
                {
                    (element as ZYTextContainer).AddElementToListAbs(myList);
                }
            }
        }

        public virtual void AddFinalElementToList(ArrayList myList)
        {
            if (myList != null)
            {
                foreach (ZYTextElement element in this.myChildElements)
                {
                    if (!element.Deleteted)
                    {
                        if (element is ZYTextContainer)
                        {
                            (element as ZYTextContainer).AddFinalElementToList(myList);
                        }
                        else
                        {
                            myList.Add(element);
                        }
                    }
                }
            }
        }

        /***
         * 最小输入长度
         * 
         */
        public virtual int minLength
        {
            get
            {
                return this.myAttributes.GetInt32("minlength");
            }
            set
            {
                this.myAttributes.SetValue("minlength", value);
            }
        }

        /***
         * 最大输入长度
         */
        public virtual int maxLength
        {
            get
            {
                return this.myAttributes.GetInt32("maxlength");
            }
            set
            {
                this.myAttributes.SetValue("maxlength", value);
            }
        }

        protected virtual void AddLastElement()
        {
            if (this.myChildElements.Count > 0)
            {
                this.myLastElement = this.myChildElements[this.myChildElements.Count - 1] as ZYTextParagraph;
                if (this.myLastElement == null)
                {
                    this.myLastElement = new ZYTextParagraph();
                    this.myChildElements.Add(this.myLastElement);
                }
            }
            else
            {
                this.myChildElements.Add(this.myLastElement);
            }
            if ((this.myLastElement != null) && (base.myOwnerDocument != null))
            {
                this.myLastElement.CreatorIndex = -1;
                this.myLastElement.DeleterIndex = -1;
            }
        }

        public virtual bool AppendChild(ZYTextElement newElement)
        {
            if (!this.bolChildElementsLocked && ((newElement != null) && !this.myChildElements.Contains(newElement)))
            {
                if (newElement.Parent != null)
                {
                    newElement.Parent.RemoveChild(newElement);
                }
                this.myChildElements.Add(newElement);
                newElement.Parent = this;
                newElement.OwnerDocument = base.myOwnerDocument;
                this.OnChildElementsChange();
                return true;
            }
            return false;
        }

        public virtual int AppendKeyValueList(ArrayList myKeyValues)
        {
            return 0;
        }


        protected virtual bool BeforeInsert(ZYTextElement NewElement)
        {
            if (this.bolChildElementsLocked)
            {
                return false;
            }
            return base.myOwnerDocument.BeforeInsertElemnt(this, NewElement);
        }

        public void ClearChild()
        {
            this.myChildElements.Clear();
            this.AddLastElement();
        }

        public void ClearSaveLog()
        {
            foreach (ZYTextElement element in this.myChildElements)
            {
                element.Visible = true;
                if (element is ZYTextContainer)
                {
                    (element as ZYTextContainer).ClearSaveLog();
                }
                else
                {
                    element.DeleterIndex = -1;
                    element.CreatorIndex = base.myOwnerDocument.SaveLogs.CurrentIndex;
                }
            }
        }

        public bool Contains(ZYTextElement vElement, bool Deep)
        {
            if (vElement != null)
            {
                if (this.myChildElements.Contains(vElement))
                {
                    return true;
                }
                if (Deep)
                {
                    foreach (ZYTextElement element in this.myChildElements)
                    {
                        if ((element is ZYTextContainer) && (element as ZYTextContainer).Contains(vElement, Deep))
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }

        public virtual bool Contains(int x, int y)
        {
            ZYTextElement zytext = base.myOwnerDocument.Content.GetElementAt(x, y);
            if (zytext is ZYTextSplit)
            {
                return true;
            }
            return this.myChildElements.Contains(zytext) || this == zytext;
        }

        public bool ContainsElements(ArrayList myList)
        {
            if ((myList != null) && (myList.Count > 0))
            {
                foreach (ZYTextElement element in myList)
                {
                    if (!this.myChildElements.Contains(element) && (element != this))
                    {
                        return false;
                    }
                }
                return true;
            }
            return false;
        }

        public virtual void DrawBackGround(ZYTextElement myElement)
        {
        }


        public bool EnumChildElements(EnumElementHandler vHandler, bool bolPreEnum)
        {
            if (vHandler != null)
            {
                if (bolPreEnum && !vHandler(base.myParent, this))
                {
                    return false;
                }
                foreach (ZYTextElement element in this.myChildElements)
                {
                    if (element is ZYTextContainer)
                    {
                        if (!(element as ZYTextContainer).EnumChildElements(vHandler, bolPreEnum))
                        {
                            return false;
                        }
                    }
                    else if (!vHandler(this, element))
                    {
                        return false;
                    }
                }
                if (!bolPreEnum && !vHandler(base.myParent, this))
                {
                    return false;
                }
                return true;
            }
            return false;
        }

        public int FixForKeyField(ArrayList myElements)
        {
            int num = 0;
            for (int i = 0; i < myElements.Count; i++)
            {
                if (!(myElements[i] is ZYTextBlock))
                {
                    continue;
                }
                ZYTextBlock block = (ZYTextBlock)myElements[i];
                if (block.KeyField && StringCommon.isBlankString(block.ToZYString()))
                {
                    for (int j = i + 1; j < myElements.Count; j++)
                    {
                        if (!(myElements[j] is ZYTextChar))
                        {
                            break;
                        }
                        //if (((ZYTextChar) myElements[j]).IsSymbol())
                        //{
                        //    myElements.RemoveRange(i + 1, j - i);
                        //    num += (j - i) - 1;
                        //    break;
                        //}
                    }
                    for (int k = i - 1; k >= 0; k--)
                    {
                        if (!(myElements[k] is ZYTextChar))
                        {
                            break;
                        }
                        if (((ZYTextChar)myElements[k]).IsSymbol())
                        {
                            myElements.RemoveRange(k + 1, (i - k) - 1);
                            num += (i - k) + 1;
                            break;
                        }
                    }
                }
            }
            return num;
        }

        public override bool FromXML(XmlElement myElement)
        {
            if (base.FromXML(myElement))
            {
                this.myChildElements.Clear();
                ArrayList myList = new ArrayList();
                base.myOwnerDocument.LoadElementsToList(myElement, myList);
                this.InsertRangeBefore(myList, null);
                //添加
                if (this is ZYTextTable || this is ZYTextTableRow) { }
                else
                {
                    this.AddLastElement();
                }
                foreach (ZYTextElement element in this.myChildElements)
                {
                    element.Parent = this;
                }
                this.strID = base.myAttributes.GetString("id");
                this.strName = base.myAttributes.GetString("name");
                return true;
            }
            return false;
        }

        internal int GetClientWidth()
        {
            return this.intClientWidth;
        }

        public virtual Rectangle GetContentBounds()
        {
            return new Rectangle(this.RealLeft, this.RealTop, this.intClientWidth, this.Height);
        }

        public virtual void GetFinalText(StringBuilder myStr)
        {
            ArrayList myElements = new ArrayList();
            foreach (ZYTextElement element in this.myChildElements)
            {
                if (!element.Deleteted)
                {
                    myElements.Add(element);
                }
            }
            //this.FixForKeyField(myElements);
            foreach (ZYTextElement element2 in myElements)
            {
                if (element2.isTextElement())
                {
                    myStr.Append(element2.ToZYString());
                }
                else if (element2 is ZYTextContainer)
                {
                    (element2 as ZYTextContainer).GetFinalText(myStr);
                }
            }
        }

        public virtual ZYTextElement GetFirstElement()
        {
            foreach (ZYTextElement element in this.myChildElements)
            {
                if (!(element is ZYTextContainer))
                {
                    return element;
                }
                ZYTextElement firstElement = (element as ZYTextContainer).GetFirstElement();
                if (firstElement != null)
                {
                    return firstElement;
                }
            }
            return null;
        }

        public virtual ZYTextElement GetLastElement()
        {
            return this.myLastElement;
        }

        protected virtual SizeF GetTitleSize()
        {
            return (SizeF)new Size(0, 0);
        }

        public virtual ArrayList GetVisibleElements()
        {
            ArrayList list = new ArrayList();
            foreach (ZYTextElement element in this.myChildElements)
            {
                if (element.Visible)
                {
                    list.Add(element);
                }
            }
            return list;
        }

        public override bool HandleClick(int x, int y, MouseButtons Button)
        {
            foreach (ZYTextElement element in this.myChildElements)
            {
                if ((element.Visible && !element.Deleteted) && element.HandleClick(x, y, Button))
                {
                    return true;
                }
            }
            return false;
        }

        public override bool HandleDblClick(int x, int y, MouseButtons Button)
        {
            foreach (ZYTextElement element in this.myChildElements)
            {
                if ((element.Visible && !element.Deleteted) && element.HandleDblClick(x, y, Button))
                {
                    return true;
                }
            }
            return false;
        }

        public override bool HandleMouseDown(int x, int y, MouseButtons Button)
        {
            foreach (ZYTextElement element in this.myChildElements)
            {
                if ((element.Visible && !element.Deleteted) && element.HandleMouseDown(x, y, Button))
                {
                    return true;
                }
            }
            return false;
        }

        public override bool HandleMouseMove(int x, int y, MouseButtons Button)
        {
            foreach (ZYTextElement element in this.myChildElements)
            {
                if ((element.Visible && !element.Deleteted) && element.HandleMouseMove(x, y, Button))
                {
                    return true;
                }
            }
            return false;
        }

        public override bool HandleMouseUp(int x, int y, MouseButtons Button)
        {
            foreach (ZYTextElement element in this.myChildElements)
            {
                if ((element.Visible && !element.Deleteted) && element.HandleMouseUp(x, y, Button))
                {
                    return true;
                }
            }
            return false;
        }

        public bool HasVisibleElement()
        {
            foreach (ZYTextElement element in this.myChildElements)
            {
                if (element.Visible)
                {
                    return true;
                }
            }
            return false;
        }

        public int IndexOf(ZYTextElement myElement)
        {
            return this.myChildElements.IndexOf(myElement);
        }

        public virtual bool InsertAfter(ZYTextElement NewElement, ZYTextElement refElement)
        {
            if (this.bolChildElementsLocked)
            {
                return false;
            }
            if ((NewElement == null) || this.Locked)
            {
                return false;
            }
            if (!this.BeforeInsert(NewElement))
            {
                return false;
            }
            if (this.myChildElements.Contains(NewElement))
            {
                this.myChildElements.Remove(NewElement);
            }
            if ((refElement != null) && this.myChildElements.Contains(refElement))
            {
                int index = this.myChildElements.IndexOf(refElement) + 1;
                if (base.myOwnerDocument.ContentChangeLog != null)
                {
                    base.myOwnerDocument.ContentChangeLog.Container = this;
                    base.myOwnerDocument.ContentChangeLog.LogInsert(index, NewElement);
                }
                this.myChildElements.Insert(index, NewElement);
            }
            else
            {
                if (base.myOwnerDocument.ContentChangeLog != null)
                {
                    base.myOwnerDocument.ContentChangeLog.Container = this;
                    base.myOwnerDocument.ContentChangeLog.LogAdd(NewElement);
                }
                this.myChildElements.Add(NewElement);
            }
            NewElement.Parent = this;
            NewElement.OwnerDocument = base.myOwnerDocument;
            NewElement.RefreshSize();
            NewElement.Visible = true;
            this.OnChildElementsChange();
            return true;
        }

        public virtual bool InsertBefore(ZYTextElement NewElement, ZYTextElement refElement)
        {
            if (this.bolChildElementsLocked == false)
            {
                this.CanDelete = this.CanDelete;
            }
            if (this.bolChildElementsLocked)
            {
                return false;
            }
            if ((NewElement == null) || this.Locked)
            {
                return false;
            }
            int maxlength = this.maxLength;
            if (maxlength > 0 && maxlength <= IntLength)
            {
                return false;
            }
            if (!this.BeforeInsert(NewElement))
            {
                return false;
            }
            if (this.myChildElements.Contains(NewElement))
            {
                this.myChildElements.Remove(NewElement);
            }
            if ((refElement != null) && this.myChildElements.Contains(refElement))
            {
                int index = this.myChildElements.IndexOf(refElement);
                if (base.myOwnerDocument.ContentChangeLog != null)
                {
                    base.myOwnerDocument.ContentChangeLog.Container = this;
                    base.myOwnerDocument.ContentChangeLog.LogInsert(index, NewElement);
                }
                this.myChildElements.Insert(index, NewElement);
            }
            else
            {
                if (base.myOwnerDocument.ContentChangeLog != null)
                {
                    base.myOwnerDocument.ContentChangeLog.Container = this;
                    base.myOwnerDocument.ContentChangeLog.LogAdd(NewElement);
                }
                this.myChildElements.Add(NewElement);
            }
            NewElement.Parent = this;
            NewElement.OwnerDocument = base.myOwnerDocument;
            NewElement.RefreshSize();
            NewElement.Visible = true;
            this.OnChildElementsChange();
            return true;
        }

        public bool InsertBeforeBapf(ZYTextElement NewElement, ZYTextElement refElement)
        {
            if (this.myChildElements.Contains(NewElement))
            {
                this.myChildElements.Remove(NewElement);
            }
            if ((refElement != null) && this.myChildElements.Contains(refElement))
            {
                int index = this.myChildElements.IndexOf(refElement);
                if (base.myOwnerDocument.ContentChangeLog != null)
                {
                    base.myOwnerDocument.ContentChangeLog.Container = this;
                    base.myOwnerDocument.ContentChangeLog.LogInsert(index, NewElement);
                }
                this.myChildElements.Insert(index, NewElement);
            }
            else
            {
                if (base.myOwnerDocument.ContentChangeLog != null)
                {
                    base.myOwnerDocument.ContentChangeLog.Container = this;
                    base.myOwnerDocument.ContentChangeLog.LogAdd(NewElement);
                }
                this.myChildElements.Add(NewElement);
            }
            NewElement.Parent = this;
            NewElement.OwnerDocument = base.myOwnerDocument;
            NewElement.RefreshSize();
            NewElement.Visible = true;
            this.OnChildElementsChange();
            return true;
        }

        public virtual bool InsertNextDiv(ZYTextElement NewElement, ZYTextElement refElement)
        {
            if (this.bolChildElementsLocked)
            {
                return false;
            }
            if ((NewElement == null) || this.Locked)
            {
                return false;
            }
            if (!this.BeforeInsert(NewElement))
            {
                return false;
            }
            if (this.myChildElements.Contains(NewElement))
            {
                this.myChildElements.Remove(NewElement);
            }
            if ((refElement != null) && this.myChildElements.Contains(refElement))
            {
                int index = this.myChildElements.IndexOf(refElement) + 1;
                if (base.myOwnerDocument.ContentChangeLog != null)
                {
                    base.myOwnerDocument.ContentChangeLog.Container = this;
                    base.myOwnerDocument.ContentChangeLog.LogInsert(index, NewElement);
                }
                this.myChildElements.Insert(index, NewElement);
            }
            else
            {
                if (base.myOwnerDocument.ContentChangeLog != null)
                {
                    base.myOwnerDocument.ContentChangeLog.Container = this;
                    base.myOwnerDocument.ContentChangeLog.LogAdd(NewElement);
                }
                this.myChildElements.Add(NewElement);
            }
            NewElement.Parent = this;
            NewElement.OwnerDocument = base.myOwnerDocument;
            NewElement.RefreshSize();
            NewElement.Visible = true;
            this.OnChildElementsChange();
            return true;
        }

        public virtual bool InsertRangeBefore(ArrayList myList, ZYTextElement refElement)
        {
            //if (this.bolChildElementsLocked == false)
            //{
            //    this.CanDelete = this.CanDelete;
            //}
            if (this.bolChildElementsLocked)
            {
                return false;
            }
            if ((myList == null) || this.Locked)
            {
                return false;
            }
            ArrayList list = new ArrayList();
            foreach (ZYTextElement element in myList)
            {
                if (element.Deleteted != true)
                {
                    if (this.BeforeInsert(element))
                    {
                        list.Add(element);
                        element.Visible = true;
                    }
                }
                else
                {
                    list.Add(element);
                }
            }
            if (list.Count > 0)
            {
                if (refElement == null)
                {
                    refElement = this.myLastElement;
                }
                if ((refElement != null) && this.myChildElements.Contains(refElement))
                {
                    if (base.myOwnerDocument.ContentChangeLog != null)
                    {
                        base.myOwnerDocument.ContentChangeLog.Container = this;
                        base.myOwnerDocument.ContentChangeLog.LogInsertRange(this.myChildElements.IndexOf(refElement), list);
                    }
                    this.myChildElements.InsertRange(this.myChildElements.IndexOf(refElement), list);
                }
                else
                {
                    if (base.myOwnerDocument.ContentChangeLog != null)
                    {
                        base.myOwnerDocument.ContentChangeLog.Container = this;
                        base.myOwnerDocument.ContentChangeLog.LogAddRang(list);
                    }
                    this.myChildElements.AddRange(list);
                }

                foreach (ZYTextElement element2 in list)
                {
                    element2.Parent = this;
                    element2.OwnerDocument = base.myOwnerDocument;
                    element2.RefreshSize();
                }
                this.OnChildElementsChange();
            }
            return true;
        }

        public override bool isField()
        {
            foreach (ZYTextElement element in this.myChildElements)
            {
                if (element is ZYTextContainer)
                {
                    return element.isField();
                }
                if (element.isField())
                {
                    return true;
                }
            }
            return false;
        }

        protected virtual bool IsLastElement(ZYTextElement vElement)
        {
            return (vElement == this.myChildElements[this.myChildElements.Count - 1]);
        }

        public override bool isNewLine()
        {
            return true;
        }

        public override bool isTextElement()
        {
            return false;
        }

        protected virtual void OnChildElementsChange()
        {
            this.RaiseOnChangeEvent();
        }

        public virtual void RaiseOnChangeEvent()
        {
            if ((base.myOwnerDocument.OwnerControl != null) && !base.myOwnerDocument.Loading)
            {
                base.myOwnerDocument.InitEventObject(ZYVBEventType.OnChange);
                base.myOwnerDocument.EventObj.Source = this;
                base.myOwnerDocument.RunEventScript(this, "onchange");
            }
        }

        protected virtual void RefreshClientWidth()
        {
            if (base.myParent == null)
            {
                if ((base.myOwnerDocument != null) && base.myOwnerDocument.Info.WordWrap)
                {
                    this.intClientWidth = ((base.myOwnerDocument.Pages.StandardWidth - this.RealLeft) - this.intLeftMargin) - this.intRightMargin;
                }
                else
                {
                    this.intClientWidth = 0;
                }
            }
            else
            {
                if (this is ZYTextTableCell)
                {
                    this.intClientWidth = (this.Width - this.intLeftMargin) - this.intRightMargin - 4;
                }
                else
                {
                    this.intClientWidth = (this.Width - this.intLeftMargin) - this.intRightMargin;
                }
            }
        }

        public virtual void RefreshLine()
        {
            if (base.myParent == null)
            {
                base.intWidth = 0;
            }
            this.RefreshLineFast(0);
        }

        int leftChars = 0;
        public virtual void RefreshLineFast(int StartIndex)
        {
            ArrayList lineElements = new ArrayList();
            int height = this.Height;
            this.TitleSize = this.GetTitleSize();
            this.RefreshClientWidth();
            int width = (int)this.TitleSize.Width;
            int index = 0;
            if ((StartIndex < 0) || (StartIndex > (this.myChildElements.Count - 1)))
            {
                StartIndex = 0;
            }
            if (StartIndex == 0)
            {
                this.myLines.Clear();
            }
            else
            {
                ZYTextElement element = this.myChildElements[StartIndex] as ZYTextElement;
                index = element.LineIndex;
                this.myLines.RemoveRange(index, this.myLines.Count - index);
                foreach (ZYTextElement element2 in this.myChildElements)
                {
                    if (element2 == element)
                    {
                        break;
                    }
                    if (element2.LineIndex == index)
                    {
                        lineElements.Add(element2);
                        width += element2.Width;
                    }
                }
            }

            bool flag = false;
            ZYTextElement element3 = null;
            this.myContentList.Clear();
            this.myContentList.AddRange(this.ChildElements);
            int hwoIndex = -1;
            bool startIsNumber = false;
            int numberlineIndex = -1;
            for (int i = StartIndex; i < this.myContentList.Count; i++)
            {
                int linesCount = this.myLines.Count;
                if (i > hwoIndex)
                {
                    for (int x = i; x < this.myContentList.Count; x++)
                    {
                        if (this.myContentList[x] is ZYTextParagraph)
                        {
                            leftChars = (this.myContentList[x] as ZYTextParagraph).LeftChars;
                            hwoIndex = x;
                            break;
                        }
                    }
                }
                ZYTextElement element4 = (ZYTextElement)this.myContentList[i];
                if (!this.myOwnerDocument.Info.ShowAll)
                {
                    if (element4.Deleteted)
                    {
                        continue;
                    }
                }

                if (this.myOwnerDocument.Info.ShowAll)
                {
                    if (element4.Sign == "delete")
                    {
                        continue;
                    }
                }

                if (element4 is ZYTextSplit)
                {
                    if (linesCount > 0)
                    {
                        ZYTextLine contanerLine = (ZYTextLine)this.myLines[linesCount - 1];
                        contanerLine.Elements.Add(element4);
                        element4.OwnerLine = contanerLine;
                    }
                    continue;
                }
                if (element4 is ZYTextSkipSign)
                {
                    if (linesCount > 0)
                    {
                        ZYTextLine contanerLine = (ZYTextLine)this.myLines[linesCount - 1];
                        contanerLine.IsSkip = true;
                    }
                    continue;
                }

                if (element4 is ZYTextDiv)
                {
                    if (((ZYTextDiv)element4).Visible)
                    {
                        continue;
                    }
                }

                if (element4.Visible)
                {
                    if (element4 is ZYTextCharTab)
                    {
                        (element4 as ZYTextCharTab).RefreshTabWidth();
                    }
                    if (element4 is ZYTextHRule)
                    {
                        element4.RefreshSize();
                    }
                    flag = true;
                    if (element4 is ZYTextContainer)
                    {
                        ZYTextContainer container = (ZYTextContainer)element4;
                        if (container.Block)
                        {
                            this.myContentList.InsertRange(this.myContentList.IndexOf(container) + 1, container.ChildElements);
                        }
                        else
                        {
                            container.RefreshLine();
                        }
                    }
                    if (element4.OwnerWholeLine() || ((element3 != null) && element3.OwnerWholeLine()))
                    {
                        flag = false;
                    }
                    else if (!element4.isNewLine() && (this.intClientWidth > 0))
                    {
                        if ((width + element4.Width + (linesCount > 0 ? leftChars : 0)) > this.intClientWidth)
                        {
                            flag = false;
                        }
                        else if (i < (this.myContentList.Count - 2))
                        {
                            ZYTextElement element5 = (ZYTextElement)this.myContentList[i + 1];
                            if ((!element5.CanBeLineHead() || !element4.CanBeLineEnd()) && (((width + element4.Width) + element5.Width + (linesCount > 0 ? leftChars : 0)) > this.intClientWidth))
                            {
                                flag = false;
                            }
                        }
                    }
                    if (lineElements.Count == 1)
                    {
                        if (lineElements[0] is ZYTextChar)
                        {
                            if (ZYTextDocument.numbers.IndexOf(((ZYTextChar)lineElements[0]).Char) >= 0)
                                startIsNumber = true;
                        }
                        else if (lineElements[0] is ZYTextBlock)
                        {
                            startIsNumber = true;
                        }
                    }
                    if (element4 is ZYTextChar && flag && i > numberlineIndex)
                    {
                        bool isStart = false;
                        ZYTextChar currentChar = (ZYTextChar)element4;
                        int howMaxWidht = width + element4.Width;
                        if (ZYTextDocument.numbers.IndexOf(currentChar.Char) >= 0 && !isStart)
                        {
                            isStart = true;
                        }
                        else
                        {
                            startIsNumber = false;
                        }
                        if (isStart && !startIsNumber)
                        {
                            for (int x = (i + 1); x < this.myContentList.Count; x++)
                            {
                                ZYTextElement element6 = (ZYTextElement)this.myContentList[x];
                                if (!this.myOwnerDocument.Info.ShowAll)
                                {
                                    if (element6.Deleteted)
                                    {
                                        continue;
                                    }
                                }
                                if (element6 is ZYTextChar)
                                {
                                    ZYTextChar charElement6 = (ZYTextChar)element6;
                                    if (ZYTextDocument.numbers.IndexOf(charElement6.Char) >= 0)
                                    {
                                        howMaxWidht += charElement6.Width;
                                        if (howMaxWidht > this.intClientWidth)
                                        {
                                            flag = false;
                                            break;
                                        }
                                        numberlineIndex = x;
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                    }

                    if (flag)
                    {
                        lineElements.Add(element4);
                        width += element4.Width;
                    }
                    else
                    {
                        this.ResetLine(lineElements);
                        lineElements.Clear();
                        width = this.intLeftMargin;
                        lineElements.Add(element4);
                        width += element4.Width;
                    }
                    if (element4.isNewLine())
                    {
                        this.ResetLine(lineElements);
                        lineElements.Clear();
                        width = this.intLeftMargin;
                    }
                }
                else
                {
                    element4.OwnerLine = null;
                }
                element3 = element4;
            }

            if (lineElements.Count > 0)
            {
                this.ResetLine(lineElements);
            }

            if (((base.myParent == null) && (base.myOwnerDocument != null)) && !base.myOwnerDocument.Info.WordWrap)
            {
                foreach (ZYTextLine line in this.myLines)
                {
                    if (base.intWidth < ((this.intLeftMargin + this.intRightMargin) + line.ContentWidth))
                    {
                        base.intWidth = (this.intLeftMargin + this.intRightMargin) + line.ContentWidth;
                    }
                }
            }
            if (this is ZYTextTable)
            {
                ZYTextTable table = (ZYTextTable)this;
                table.RefreshRowHeight();
            }

            if (this is ZYTextTableRow)
            {
                ZYTextTableRow row = (ZYTextTableRow)this;
                row.RefreshRowHeight();
            }
            this.UpdateBounds();
            if ((this.Height != height) && !base.myOwnerDocument.Loading)
            {
                base.myOwnerDocument.RefreshAllFlag = true;
            }
        }

        /// <summary>
        /// 有问题
        /// </summary>
        /// <returns></returns>
        public override bool RefreshSize()
        {
            foreach (ZYTextElement element in this.myChildElements)
            {
                element.RefreshSize();
            }
            return true;
        }

        public override bool RefreshView()
        {
            Rectangle contentBounds = this.GetContentBounds();
            if (base.myOwnerDocument.isNeedDraw(contentBounds) && this.HasVisibleElement())
            {
                foreach (ZYTextElement element in this.myChildElements)
                {
                    if (element is ZYTextContainer)
                    {
                        ((ZYTextContainer)element).ResetViewState();
                    }
                }
                ArrayList list = new ArrayList();
                this.RefreshClientWidth();
                int realTop = this.RealTop;
                int realLeft = this.RealLeft;
                ZYTextElement zl = base.OwnerDocument.JumpElement;
                foreach (ZYTextLine line in this.myLines)
                {
                    int y = realTop + line.Top;
                    int height = line.Height;
                    int num5 = line.RealLeft;
                    if (base.OwnerDocument.EnableJumpPrint)
                    {
                        if (zl != null && !zl.Deleteted)
                        {
                            if (y + height < zl.RealTop)
                            {
                                continue;
                            }
                        }
                    }
                    Rectangle rect = new Rectangle(0, y, 0, height);
                    if (base.myOwnerDocument.View.isNeedDrawY(y, height))
                    {
                        bool flag = false;
                        foreach (ZYTextElement element2 in line.Elements)
                        {
                            int intLeft = num5 + element2.Left;
                            int vTop = y + element2.Top;
                            int intWidth = element2.Width + element2.WidthFix;
                            int vHeight = element2.Height;
                            if (base.myOwnerDocument.View.isNeedDrawX(intLeft, intWidth))
                            {
                                flag = true;
                            }
                            else if (element2 is ZYTextContainer)
                            {
                                Rectangle rectangle3 = (element2 as ZYTextContainer).GetContentBounds();
                                if (!rectangle3.IsEmpty && base.myOwnerDocument.View.isNeedDrawX(rectangle3.Left, rectangle3.Width))
                                {
                                    flag = true;
                                }
                            }
                            if (flag)
                            {
                                if (base.myOwnerDocument.isSelected(element2))
                                {
                                    if (rect.Width == 0)
                                    {
                                        rect.X = intLeft;
                                        rect.Y = line.RealTop;
                                        rect.Width = element2.Width + element2.WidthFix;
                                    }
                                    else
                                    {
                                        if (rect.Left > intLeft)
                                        {
                                            rect.X = intLeft;
                                        }
                                        if (rect.Right < ((intLeft + element2.Width) + element2.WidthFix))
                                        {
                                            rect.Width = ((intLeft + element2.Width) + element2.WidthFix) - rect.Left;
                                        }
                                    }
                                }
                                else if (base.myOwnerDocument.Info.ShowMark)
                                {
                                    int markLevel = base.myOwnerDocument.GetMarkLevel(element2.CreatorIndex);
                                    if (markLevel > 0)
                                    {
                                        base.myOwnerDocument.DrawNewBackGround(markLevel, intLeft, vTop, intWidth, vHeight);
                                    }
                                    else if (element2.isField() && (element2 is ZYTextChar))
                                    {
                                        base.myOwnerDocument.View.DrawFieldBackGround(intLeft, vTop, intWidth, vHeight);
                                    }
                                }
                                element2.RefreshView();
                                element2.Parent.DrawBackGround(element2);
                                if (element2.Deleteted)
                                {
                                    base.myOwnerDocument.DrawDeleteLine(base.myOwnerDocument.GetMarkLevel(element2.DeleterIndex), element2.Bounds.Left, element2.Bounds.Top, element2.Width, element2.Height);
                                }
                            }
                        }
                        if (rect.Width > 0)
                        {
                            base.myOwnerDocument.View.InvertRect(rect);
                        }
                    }
                }
                base.myBorder.Draw(base.myOwnerDocument.View, contentBounds);
                return true;
            }
            return false;

        }

        public virtual int RemoveBlankKeyField(bool ContentLog)
        {
            ArrayList list = new ArrayList();
            int num = 0;
            for (int i = 0; i < this.myChildElements.Count; i++)
            {
                ZYTextElement element = (ZYTextElement)this.myChildElements[i];
                if (!element.Deleteted && (element is ZYTextBlock))
                {
                    ZYTextBlock block = (ZYTextBlock)element;
                    if (!block.KeyField || !StringCommon.isBlankString(block.ToZYString()))
                    {
                        continue;
                    }
                    int index = 0;
                    int num4 = this.myChildElements.Count - 2;
                    ZYTextChar ch = null;
                    ZYTextChar ch2 = null;
                    for (int j = this.myChildElements.IndexOf(block); j < this.myChildElements.Count; j++)
                    {
                        if ((this.myChildElements[j] is ZYTextChar) && ((ZYTextChar)this.myChildElements[j]).IsSymbol())
                        {
                            num4 = j;
                            ch2 = (ZYTextChar)this.myChildElements[j];
                            break;
                        }
                    }
                    for (int k = this.myChildElements.IndexOf(block); k >= 0; k--)
                    {
                        if ((this.myChildElements[k] is ZYTextChar) && ((ZYTextChar)this.myChildElements[k]).IsSymbol())
                        {
                            index = k;
                            ch = (ZYTextChar)this.myChildElements[k];
                            break;
                        }
                    }
                    bool flag = true;
                    if (((ch != null) && (ch2 != null)) && (StringCommon.GetSymbolSplitLevel(ch.Char) < StringCommon.GetSymbolSplitLevel(ch2.Char)))
                    {
                        flag = false;
                    }
                    if (flag)
                    {
                        index++;
                    }
                    else
                    {
                        num4--;
                    }
                    if (num4 >= index)
                    {
                        if (ContentLog)
                        {
                            base.myOwnerDocument.BeginContentChangeLog();
                        }
                        if (base.myOwnerDocument.CanContentChangeLog)
                        {
                            base.myOwnerDocument.ContentChangeLog.Container = this;
                        }
                        for (int m = index; m <= num4; m++)
                        {
                            if (base.myOwnerDocument.CanContentChangeLog)
                            {
                                base.myOwnerDocument.ContentChangeLog.LogRemove(index, (ZYTextElement)this.myChildElements[index]);
                            }
                            this.myChildElements.RemoveAt(index);
                            num++;
                        }
                        if (ContentLog)
                        {
                            base.myOwnerDocument.EndContentChangeLog();
                        }
                        i = index - 1;
                    }
                    continue;
                }
                if (element is ZYTextContainer)
                {
                    num += ((ZYTextContainer)element).RemoveBlankKeyField(ContentLog);
                }
            }
            return num;
        }

        public virtual int RemoveBlankKeyField2(bool ContentLog)
        {
            ArrayList list = new ArrayList();
            ArrayList myList = new ArrayList();
            int num = 0;
            bool flag = true;
            int num2 = 0;
            for (int i = 0; i < this.myChildElements.Count; i++)
            {
                bool flag2 = false;
                ZYTextElement vElement = (ZYTextElement)this.myChildElements[i];
                if (this.IsLastElement(vElement))
                {
                    flag2 = true;
                }
                else if (vElement is ZYTextChar)
                {
                    ZYTextChar ch = (ZYTextChar)vElement;
                    if (ch.IsSymbol() && (myList.Count > 0))
                    {
                        flag2 = true;
                    }
                    myList.Add(vElement);
                }
                else if (vElement is ZYTextBlock)
                {
                    ZYTextBlock block = (ZYTextBlock)vElement;
                    if (!block.KeyField || StringCommon.HasContent(block.ToZYString()))
                    {
                        flag = false;
                    }
                    num2++;
                    myList.Add(vElement);
                }
                else
                {
                    if (vElement is ZYTextContainer)
                    {
                        num += ((ZYTextContainer)vElement).RemoveBlankKeyField2(ContentLog);
                        continue;
                    }
                    flag2 = true;
                }
                if (flag2)
                {
                    if (((myList.Count > 0) && flag) && (num2 > 0))
                    {
                        ZYTextChar ch2 = myList[0] as ZYTextChar;
                        ZYTextChar ch3 = myList[myList.Count - 1] as ZYTextChar;
                        if (((myList.Count > 1) && (ch2 != null)) && (ch3 != null))
                        {
                            int symbolSplitLevel = StringCommon.GetSymbolSplitLevel(ch2.Char);
                            int num5 = StringCommon.GetSymbolSplitLevel(ch3.Char);
                            if ((symbolSplitLevel != 0) && (num5 != 0))
                            {
                                myList.RemoveAt((symbolSplitLevel < num5) ? (myList.Count - 1) : 0);
                            }
                        }
                        if (myList.Count > 0)
                        {
                            int index = this.myChildElements.IndexOf(myList[0]);
                            if (ContentLog)
                            {
                                base.myOwnerDocument.BeginContentChangeLog();
                                base.myOwnerDocument.ContentChangeLog.Container = this;
                                base.myOwnerDocument.ContentChangeLog.LogRemoveRange(index, myList);
                                base.myOwnerDocument.EndContentChangeLog();
                            }
                            this.myChildElements.RemoveRange(index, myList.Count);
                            num += myList.Count;
                            i = index - 1;
                        }
                    }
                    num2 = 0;
                    myList.Clear();
                    flag = true;
                }
            }
            return num;
        }

        public virtual int RemoveBlankLine()
        {
            int num = 0;
            for (int i = 0; i < (this.myChildElements.Count - 1); i++)
            {
                ZYTextElement myElement = (ZYTextElement)this.myChildElements[i];
                if ((myElement is ZYTextParagraph) && (myElement.OwnerLine.Elements.Count == 1))
                {
                    base.myOwnerDocument.BeginContentChangeLog();
                    base.myOwnerDocument.ContentChangeLog.Container = this;
                    base.myOwnerDocument.ContentChangeLog.LogRemove(i, myElement);
                    base.myOwnerDocument.EndContentChangeLog();
                    num++;
                    i--;
                }
                else if (myElement is ZYTextContainer)
                {
                    num += (myElement as ZYTextContainer).RemoveBlankLine();
                }
            }
            return num;
        }

        public virtual bool RemoveChild(ZYTextElement refElement)
        {
            if (!this.bolChildElementsLocked && ((!this.Locked && (refElement != null)) && (this.myChildElements.Contains(refElement) && (refElement != this.myLastElement))))
            {
                if (base.myOwnerDocument.CanContentChangeLog)
                {
                    base.myOwnerDocument.ContentChangeLog.Container = this;
                    base.myOwnerDocument.ContentChangeLog.LogRemove(this.myChildElements.IndexOf(refElement), refElement);
                }
                this.myChildElements.Remove(refElement);
                this.OnChildElementsChange();
                return true;
            }
            return false;
        }

        public virtual int RemoveChildRange(ArrayList myList)
        {
            if (this.bolChildElementsLocked)
            {
                return 0;
            }
            int num = 0;
            if ((!this.Locked && (myList != null)) && (myList.Count > 0))
            {
                foreach (ZYTextElement element in myList)
                {
                    if (this.myChildElements.Contains(element) && (element != this.myLastElement))
                    {
                        if (base.myOwnerDocument.CanContentChangeLog)
                        {
                            base.myOwnerDocument.ContentChangeLog.Container = this;
                            base.myOwnerDocument.ContentChangeLog.LogRemove(this.myChildElements.IndexOf(element), element);
                        }
                        this.myChildElements.Remove(element);
                        num++;
                    }
                }
                if (num > 0)
                {
                    this.OnChildElementsChange();
                }
            }
            return num;
        }

        private int ResetLine(ArrayList LineElements)
        {
            int lineElementsCount = LineElements.Count;
            if (base.OwnerDocument.IgnoreLine && base.OwnerDocument.Info.Printing)
            {
                if (lineElementsCount == 1 && LineElements[0] is ZYTextParagraph && (this.myLines.Count >= 1 || this.TitleLine == true))
                {
                    return 0;
                }
            }
            if (lineElementsCount == 0)
            {
                return 0;
            }
            ZYTextLine line = new ZYTextLine();
            line.Index = this.myLines.Count;
            line.Container = this;
            line.RealLeft = this.RealLeft;
            line.Elements.AddRange(LineElements);
            foreach (ZYTextElement element in LineElements)
            {
                if ((element is ZYTextTableCell) && ((ZYTextTableCell)element).VMStart)
                {
                    line.ContentWidth += element.Width;
                }
                else
                {
                    if (line.Height < element.Height)
                    {
                        line.Height = element.Height;
                    }
                    line.ContentWidth += element.Width;
                }
            }
            if (((this.myLines.Count == 1) && !this.TitleLine) && (line.Height < ((int)this.TitleSize.Height)))
            {
                line.Height = (int)this.TitleSize.Height;
            }
            if (line.Height < 13)
            {
                line.Height = base.myOwnerDocument.View.DefaultRowHeight();
            }
            this.myLines.Add(line);
            base.myOwnerDocument.AddLine(line);
            int intClientWidth = this.intClientWidth - this.leftChars;
            if (this.myLines.Count == 1)
            {
                intClientWidth -= (int)this.TitleSize.Width;
            }
            ParagraphAlignConst left = ParagraphAlignConst.Left;
            int intLeftMargin = 0;
            if (line.HasLineEnd)
            {
                for (int i = this.myContentList.IndexOf(LineElements[LineElements.Count - 1]); i < this.myContentList.Count; i++)
                {
                    if (this.myContentList[i] is ZYTextParagraph)
                    {
                        left = (this.myContentList[i] as ZYTextParagraph).Align;
                        break;
                    }
                }
            }
            switch (left)
            {
                case ParagraphAlignConst.Left:
                    intLeftMargin = this.intLeftMargin;
                    break;

                case ParagraphAlignConst.Right:
                    intLeftMargin = (this.intLeftMargin + intClientWidth) - line.ContentWidth;
                    break;

                case ParagraphAlignConst.Center:
                    intLeftMargin = this.intLeftMargin + ((intClientWidth - line.ContentWidth) / 2);
                    break;

                case ParagraphAlignConst.Justify:
                    intLeftMargin = this.intLeftMargin;
                    break;
            }
            if (this.Lines.Count != 1)
                intLeftMargin += leftChars;
            if (this.myLines.Count == 1)
            {
                intLeftMargin += (int)this.TitleSize.Width;
            }
            int num4 = 0;
            int num5 = 0;
            if ((LineElements.Count > 0) && (!line.HasLineEnd || (left == ParagraphAlignConst.Justify)))
            {
                num5 = intClientWidth - line.ContentWidth;
                num4 = (int)Math.Ceiling((double)(((double)num5) / ((double)(LineElements.Count - 1))));
                if ((num5 > 0) && (num4 < 1))
                {
                    num4 = 1;
                }
            }
            if (this is ZYTextTableCell)
            {
                intLeftMargin += ((ZYTextTableCell)this).InnerLeftMargin + 2;
            }
            int x = 0;
            int y = (int)(lineElementsCount / 2 - num5 / 2);
            foreach (ZYTextElement element2 in LineElements)
            {
                x++;
                element2.OwnerLine = line;
                element2.Left = intLeftMargin;
                if ((element2 is ZYTextTableCell) && ((ZYTextTableCell)element2).VMStart)
                {
                    element2.Top = line.Top;
                }
                else if (this is ZYTextTableCell)
                {
                    element2.Top = (line.Height - element2.Height) + ((ZYTextTableCell)this).InnerTopMargin;
                }
                else
                {
                    //  if (element2 is ZYTextImage)
                    element2.Top = (line.Height - element2.Height) / 2;
                    //if (element2 is ZYTextParagraph && LineElements[0] != null && LineElements[0] is ZYTextTable)
                    //    element2.Top = line.Height - element2.Height - 10;
                    //else
                    //    element2.Top = (line.Height - element2.Height);
                    //if (element2 is ZYTextTableCell)
                    //{
                    //    element2.Top = 0;
                    //}
                }
                element2.WidthFix = 0;
                intLeftMargin += element2.Width;
                if (x > y && (num4 > 0) && (element2 is ZYTextChar))
                {
                    intLeftMargin += num4;
                    element2.WidthFix = num4;
                    num5 -= num4;
                    if (num5 <= 0)
                    {
                        num4 = 0;
                    }
                }
            }
            if (((base.myParent == null) && !base.myOwnerDocument.Info.WordWrap) && (base.intWidth < intLeftMargin))
            {
                base.intWidth = intLeftMargin;
            }
            line.RealIndex = line.CalcuteRealIndex();
            if (!(line.LastElement is ZYTextTableRow))
            {
                line.Height += (int)(base.myOwnerDocument.LineSpacing * 2.835f);
            }
            return line.Height;
        }

        public virtual void ResetViewState()
        {
        }

        public override string ToZYString()
        {
            if (this.myChildElements.Count == 0)
            {
                return null;
            }
            ArrayList visibleElements = this.GetVisibleElements();
            this.FixForKeyField(visibleElements);
            StringBuilder builder = new StringBuilder(this.myChildElements.Count);
            foreach (ZYTextElement element in visibleElements)
            {
                builder.Append(element.ToZYString());
            }
            return builder.ToString();
        }

        public override bool ToXML(XmlElement myElement)
        {
            XmlElement newChild = null;
            switch (base.myOwnerDocument.Info.SaveMode)
            {
                case 0:
                    {
                        ZYTextElement element2 = null;
                        ZYTextChar ch = null;
                        ZYTextChar ch2 = null;
                        base.myAttributes.ToXML(myElement);
                        if (base.myOwnerDocument.Info.SavePreViewText)
                        {
                            ArrayList myList = new ArrayList();
                            this.AddFinalElementToList(myList);
                            string elementsText = ZYTextElement.GetElementsText(myList);
                            newChild = myElement.OwnerDocument.CreateElement("text");
                            myElement.AppendChild(newChild);
                            newChild.InnerText = elementsText;
                        }
                        foreach (ZYTextElement element3 in this.myChildElements)
                        {
                            if (base.OwnerDocument.IsHaveDeleted)
                            {
                                element3.CreatorIndex = 0;
                                element3.OperaterCreater = "0";
                                if (element3.Deleteted)
                                {
                                    continue;
                                }
                            }
                            bool flag = false;
                            ch2 = element3 as ZYTextChar;
                            //（新增）*******还原字符最初的颜色
                            if (ch2 != null)
                            {
                                ch2.ForeColor = ch2.TempForeColor;
                            }

                            if (ch2 == null)
                            {
                                ch = null;
                            }
                            else
                            {
                                if ((ch != null) && ch.Attributes.EqualsValue(ch2.Attributes))
                                {
                                    ch2.AppendToXML(newChild);
                                    flag = true;
                                }
                                ch = ch2;
                            }
                            if (!flag && (element3.GetXMLName() != null && element3.GetXMLName() != ""))
                            {
                                newChild = myElement.OwnerDocument.CreateElement(element3.GetXMLName());
                                myElement.AppendChild(newChild);
                                element3.ToXML(newChild);
                                element2 = element3;
                            }
                        }
                        break;
                    }
                case 1:
                    myElement.SetAttribute("name", base.myAttributes.GetString("name"));
                    foreach (ZYTextElement element4 in this.myChildElements)
                    {
                        if (element4.DeleterIndex >= 0)
                        {
                            continue;
                        }
                        if (((element4 is ZYTextChar) || (element4 is ZYTextParagraph)) || (element4 is ZYTextLineEnd))
                        {
                            myElement.AppendChild(myElement.OwnerDocument.CreateTextNode(element4.ToZYString()));
                            continue;
                        }
                        if (element4.isField() && (element4.GetXMLName() != null))
                        {
                            newChild = myElement.OwnerDocument.CreateElement(element4.GetXMLName());
                            myElement.AppendChild(newChild);
                            element4.ToXML(newChild);
                        }
                    }
                    break;

                case 2:
                    myElement.SetAttribute("name", base.myAttributes.GetString("name"));
                    foreach (ZYTextElement element5 in this.myChildElements)
                    {
                        if (((element5.DeleterIndex < 0) && element5.isField()) && (element5.GetXMLName() != null))
                        {
                            newChild = myElement.OwnerDocument.CreateElement(element5.GetXMLName());
                            myElement.AppendChild(newChild);
                            element5.ToXML(newChild);
                        }
                    }
                    break;

                case 3:
                    {
                        ArrayList list2 = new ArrayList();
                        this.AddFinalElementToList(list2);
                        string str3 = ZYTextElement.GetElementsText(list2);
                        newChild = myElement.OwnerDocument.CreateElement("text");
                        myElement.AppendChild(newChild);
                        newChild.InnerText = str3;
                        foreach (ZYTextElement element6 in this.myChildElements)
                        {
                            if ((element6.DeleterIndex < 0) && element6.isField())
                            {
                                if (StringCommon.isBlankString(element6.Attributes.GetString("id")))
                                {
                                    element6.Attributes.SetValue("id", StringCommon.AllocObjectName());
                                }
                                newChild = myElement.OwnerDocument.CreateElement(element6.Attributes.GetString("id"));
                                myElement.AppendChild(newChild);
                                element6.ToXML(newChild);
                            }
                        }
                        break;
                    }
                default:
                    return false;
            }
            return true;
        }

        public override void UpdateAttrubute()
        {
            this.strID = base.myAttributes.GetString("id");
            this.strName = base.myAttributes.GetString("name");
        }

        public virtual void UpdateBounds()
        {
            int height = 0;
            if (this.TitleLine)
            {
                height = (int)this.GetTitleSize().Height;
            }
            Rectangle empty = Rectangle.Empty;
            foreach (ZYTextLine line in this.myLines)
            {
                if (line.LastElement is ZYTextTable)
                {
                    line.LineSpacing = base.myOwnerDocument.Info.TableSapcing;
                }
                else if (line.LastElement is ZYTextTableRow)
                {
                    line.LineSpacing = 0;
                }
                else if (line.Container is ZYTextTableRow)
                {
                    line.LineSpacing = 0;
                }
                else if ((line.LastElement != null) && line.LastElement.isNewParagraph())
                {
                    line.LineSpacing = base.myOwnerDocument.Info.ParagraphSpacing;
                }
                else
                {
                    line.LineSpacing = base.myOwnerDocument.Info.LineSpacing;
                }
                if (line.Container is ZYTextTableCell)
                {
                    ZYTextTableCell container = (ZYTextTableCell)line.Container;
                    if (container.Align == TableCellAlign.Top)
                    {
                        line.Top = height + base.OwnerDocument.Info.LineSpacing / 2;
                        line.RealTop = this.RealTop + height + base.OwnerDocument.Info.LineSpacing / 2;
                        line.RealLeft = this.RealLeft;
                    }
                    else if (container.Align == TableCellAlign.Center)
                    {
                        line.Top = height + ((container.Height - container.RealHeight) / 2) + base.OwnerDocument.Info.LineSpacing / 2;
                        line.RealTop = this.RealTop + line.Top;
                        line.RealLeft = this.RealLeft;
                    }
                    else if (container.Align == TableCellAlign.Bottom)
                    {
                        line.Top = height + (container.Height - container.RealHeight) + base.OwnerDocument.Info.LineSpacing / 2;  // container.Height - height;//(height + container.Height) + line.FullHeight;
                        line.RealTop = this.RealTop + line.Top;//this.RealTop + line.Top;
                        line.RealLeft = this.RealLeft;
                    }
                }
                else
                {
                    line.Top = height;
                    line.RealTop = this.RealTop + height;
                    line.RealLeft = this.RealLeft;
                }
                height += line.FullHeight;
                foreach (ZYTextElement element in line.Elements)
                {
                    empty = new Rectangle(element.RealLeft, element.RealTop, element.Width + element.WidthFix, element.Height);
                    if (!empty.Equals(element.Bounds))
                    {
                        if ((base.myOwnerDocument.OwnerControl != null) && !(element is ZYTextContainer))
                        {
                            if (!element.Bounds.IsEmpty)
                            {
                                base.myOwnerDocument.OwnerControl.AddInvalidateRect(element.Bounds);
                            }
                            base.myOwnerDocument.OwnerControl.AddInvalidateRect(empty);
                        }
                        element.Bounds = empty;
                    }
                    if (element is ZYTextContainer)
                    {
                        ((ZYTextContainer)element).UpdateBounds();
                    }
                }
            }
        }

        public virtual void UpdateUserLogin()
        {
            foreach (ZYTextElement element in this.myChildElements)
            {
                if (element is ZYTextContainer)
                {
                    (element as ZYTextContainer).UpdateUserLogin();
                }
            }
        }

        public virtual bool Block
        {
            get
            {
                return false;
            }
        }

        public int ChildCount
        {
            get
            {
                return this.myChildElements.Count;
            }
        }

        public ArrayList ChildElements
        {
            get
            {
                return this.myChildElements;
            }
        }

        public override bool Deleteted
        {
            get
            {
                return base.Deleteted;
            }
            set
            {
                base.Deleteted = value;
                foreach (ZYTextElement element in this.myChildElements)
                {
                    element.Deleteted = value;
                }
            }
        }

        public virtual bool EnableTypeSet
        {
            get
            {
                return false;
            }
        }

        public override int Height
        {
            get
            {
                if (this.myLines.Count > 0)
                {
                    ZYTextLine line = (ZYTextLine)this.myLines[this.myLines.Count - 1];
                    return (((line.Top + line.Height) + this.intTopMargin) + this.intBottomMargin);
                }
                return ((base.myOwnerDocument.View.DefaultRowHeight() + this.intTopMargin) + this.intBottomMargin);
            }
            set
            {
            }
        }

        public string ID
        {
            get
            {
                return this.strID;
            }
            set
            {
                base.myAttributes.SetValue("id", value);
                this.strID = value;
            }
        }

        public ArrayList Lines
        {
            get
            {
                return this.myLines;
            }
        }

        public int LineSpan
        {
            get
            {
                return this.intLineSpan;
            }
            set
            {
                this.intLineSpan = value;
            }
        }

        public virtual bool Locked
        {
            get
            {
                if (base.myOwnerDocument.Loading)
                {
                    return false;
                }
                return (base.myOwnerDocument.Locked || this.bolLocked);
            }
            set
            {
                this.bolLocked = value;
            }
        }

        public int MaxWidth
        {
            get
            {
                return this.intMaxWidth;
            }
            set
            {
                this.intMaxWidth = value;
            }
        }

        public string Name
        {
            get
            {
                return this.strName;
            }
            set
            {
                base.myAttributes.SetValue("name", value);
                this.strName = value;
            }
        }

        public override ZYTextDocument OwnerDocument
        {
            get
            {
                return base.myOwnerDocument;
            }
            set
            {
                base.myOwnerDocument = value;
                if (this.myLastElement != null)
                {
                    this.myLastElement.OwnerDocument = value;
                }
                foreach (ZYTextElement element in this.myChildElements)
                {
                    element.OwnerDocument = value;
                }
            }
        }

        public bool TitleLine
        {
            get
            {
                return base.myAttributes.GetBool("titleline");
            }
            set
            {
                base.myAttributes.SetValue("titleline", value);
            }
        }

        public string ValueSource
        {
            get
            {
                return base.myAttributes.GetString("valuesource");
            }
            set
            {
                base.myAttributes.SetValue("valuesource", value);
            }
        }

        internal ZYTextElement vLastElement
        {
            get
            {
                return this.myLastElement;
            }
        }

        public virtual bool WholeElement
        {
            get
            {
                return false;
            }
        }
    }
}

